﻿using System;
using System.Web.Mvc;
using Xenta.Attributes;
using Xenta.Utils;

namespace Xenta.Web.Controllers
{
    [Authenticate, InRole("ADMIN")]
    public sealed class RelationController : ExtController
    {
        [PersistState]
        public ActionResult Map(string operation, 
            int entityID, 
            int childID, 
            string returnUrl)
        {
            try
            {
                Svc.ProcessOperation(operation, new
                {
                    EntityID = entityID,
                    ChildID = childID
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return Redirect(returnUrl);
        }

        [PersistState]
        public ActionResult Unmap(string operation, 
            int entityID, 
            int childID, 
            string returnUrl)
        {
            try
            {
                Svc.ProcessOperation(operation, new
                {
                    EntityID = entityID,
                    ChildID = childID
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return Redirect(returnUrl);
        }
    }
}